package com.matrix.tool.ui.tool

import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.blankj.utilcode.util.UriUtils
import com.matrix.basecore.arouter.ARouterUtils
import com.matrix.basecore.base.BaseActivity
import com.matrix.basecore.constant.Constant
import com.matrix.tool.R
import com.matrix.tool.bean.BaseMultiQuickItem
import com.matrix.tool.bean.PhotoPathInfo
import com.matrix.tool.databinding.ActivityGifphotoBinding
import com.matrix.tool.ui.tool.adapter.PhotoAdapter

@Route(path = Constant.ARouterPath.GIFPhotoActivityPath)
class GIFPhotoActivity : BaseActivity<ActivityGifphotoBinding>() {

    private var paths: MutableList<PhotoPathInfo> = mutableListOf()
    private lateinit var adapter: PhotoAdapter
    private var replacePosition = 0
    private var deletePosition = 0

    //新增
    private val openPhotoAddLauncher =
        registerForActivityResult(ActivityResultContracts.GetContent()) { uri ->
            uri?.let {
                var ppi = PhotoPathInfo(BaseMultiQuickItem.SECOND_TYPE)
                ppi.path = UriUtils.uri2File(it).path
                if (ppi.path.endsWith("png") || ppi.path.endsWith("jpg")) {
                    paths.add(paths.size - 1, ppi)
                    adapter.setList(paths)
                } else {
                    ToastUtils.showShort("请选择图片")
                }
            } ?: ToastUtils.showShort("请选择图片")
        }

    //替换
    private val openPhotoReplaceLauncher =
        registerForActivityResult(ActivityResultContracts.GetContent()) { uri ->
            uri?.let {
                var ppi = PhotoPathInfo(BaseMultiQuickItem.SECOND_TYPE)
                ppi.path = UriUtils.uri2File(it).path
                paths.removeAt(replacePosition)
                paths.add(replacePosition, ppi)
                adapter.setList(paths)
            } ?: ToastUtils.showShort("请选择图片")
        }

    override fun getViewBinding(): ActivityGifphotoBinding {
        enableEdgeToEdge()
        return ActivityGifphotoBinding.inflate(layoutInflater)
    }

    override fun initView() {
        var grid = GridLayoutManager(this, 3)
        mViewBinding.recyclerview.layoutManager = grid
        paths.add(PhotoPathInfo(BaseMultiQuickItem.FIRST_TYPE))
        adapter = PhotoAdapter(paths)
        mViewBinding.recyclerview.adapter = adapter
    }

    override fun initListener() {
        mViewBinding.ivBack.setOnClickListener {
            finish()
        }
        adapter.setOnItemChildClickListener { _, view, position ->
            //add
            if (position == paths.size - 1) {
                if (paths.size < 10) {
                    openPhotoAddLauncher.launch("image/*")
                } else {
                    ToastUtils.showShort("最多选择9张图片")
                }
            } else {
                when (view.id) {
                    R.id.iv_image -> openPhoto(position)
                    R.id.iv_delete -> deletePath(position)
                }
            }
        }
        mViewBinding.tvComplete.setOnClickListener {
            if (paths.size > 1) {
                var path = ArrayList<String>()
                paths.removeAt(paths.size - 1)
                for (item in paths) {
                    path.add(item.path)
                }
                ARouterUtils.startGIFCorpActivity(path)
//                ARouterUtils.startCorpTestActivity(path)
                finish()
            } else {
                ToastUtils.showShort("请选择图片")
            }
        }
    }

    private fun openPhoto(position: Int) {
        replacePosition = position
        openPhotoReplaceLauncher.launch("image/*")
    }

    private fun deletePath(position: Int) {
        deletePosition = position
        paths.removeAt(deletePosition)
        adapter.setList(paths)
    }
}